#include "response.h"

Response::Response()
{
    m_status = 0;
    m_code = 0;
}

Response::Response(QNetworkReply *reply)
{
    m_status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
    m_head_ContentLength = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong();
    m_head_Location = reply->header(QNetworkRequest::LocationHeader).toString();
}

Response::Response(int status)
{
    m_status = status;
}

Response::Response(int status, const QString &content, QVariantMap headers)
{
    m_status = status;
    m_content = content.toUtf8();
    m_document = QJsonDocument::fromJson(m_content);
    m_code = m_document.object()["code"].toInt();
    m_msg = m_document.object()["msg"].toString();
    m_data = m_document.object()["data"].toVariant();
    m_headers = headers;
}

Response::Response(int status, const QVariant &variant, QVariantMap headers)
{
    m_status = status;
    m_document = QJsonDocument::fromVariant(variant);
    m_content = m_document.toJson();
    m_code = m_document.object()["code"].toInt();
    m_msg = m_document.object()["msg"].toString();
    m_data = m_document.object()["data"].toVariant();
    m_headers = headers;
}

Response::Response(int status, const QByteArray &content, QVariantMap headers)
{
    m_status = status;
    m_content = content;
    m_document = QJsonDocument::fromJson(content);
    m_code = m_document.object()["code"].toInt();
    m_msg = m_document.object()["msg"].toString();
    m_data = m_document.object()["data"].toVariant();
    m_headers = headers;
}

bool Response::ok()
{
    return m_status == 200;
}

int Response::status()
{
    return m_status;
}


QByteArray Response::content() const
{
    return m_content;
}


int Response::code()
{
    return m_code;
}

QString Response::msg()
{
    return m_msg;
}

QVariant Response::data()
{
    return m_data;
}

QString Response::toJson()
{
    return m_document.toJson();
}

QString Response::toString()
{
    return m_content;
}

QVariantMap Response::toMap()
{
    return m_document.object().toVariantMap();
}

QVariantList Response::toList()
{
    return m_document.array().toVariantList();
}

void Response::setCookies(QList<QNetworkCookie> &cookies)
{
    m_cookies = cookies;
}

QList<QNetworkCookie> Response::cookies()
{
    return m_cookies;
}

qint64 Response::headContentLength() const
{
    return m_head_ContentLength;
}

QVariantMap Response::headers() const
{
    return m_headers;
}

QString Response::location() const
{
    return m_head_Location;
}

void Response::setLocation(const QString &newLocation)
{
    m_head_Location = newLocation;
}
